#include <iostream>

using namespace std;

typedef long long LL;

const int N = 1010, M = 110, K = 150;

int k, f[N][M][K];
LL x;

void add(int C[], int A[], int B[])
{
	for (int i = 0; i < K - 1; i++)
	{
		C[i] += A[i] + B[i];
		C[i + 1] += C[i] / 10;
		C[i] %= 10;
	}
}

LL qpow(LL a, LL b, LL p)
{
	a %= p;
	LL ret = 1;
	while (b)
	{
		if (b & 1) ret = ret * a % p;
		b >>= 1;
		a = a * a % p;
	}
	return ret;
}

int main()
{
	cin >> k >> x;
	k--;
	x = qpow(x, x, 1000) - 1;
//		cout << k << " " << x << endl;
		// f[i][j] = f[i - 1][j - 1] + f[i - 1][j]
	for (int i = 0; i <= x; i++)
	{
		f[i][0][0] = 1;
		for (int j = 1; j <= min(i, k); j++)
		{
			add(f[i][j], f[i - 1][j - 1], f[i - 1][j]);
		}
	}

	int pos = K - 1;
	while (f[x][k][pos] == 0) pos--;
	for (int i = pos; i >= 0; i--) cout << f[x][k][i];

	return 0;
}
